//
//  HandleWarnViewController.m
//  Rail
//
//  Created by hychen on 2018/2/28.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "HandleWarnViewController.h"
#import "YCTextView.h"

@interface HandleWarnViewController ()<YCTextViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *textContainer;
@property (nonatomic, strong) YCTextView *textView;
@end

@implementation HandleWarnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"处理报警"];
    [self prepareTextView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)prepareTextView{
    self.textView = [[YCTextView alloc] init];
    self.textView.font = [UIFont systemFontOfSize:14.f];
    self.textView.delegate = self;
    self.textView.limitTextLength = 80;
    self.textView.limitStyle = LimitStyleExistAndAll;
    self.textView.placeholder = @"请输入意见";
    [self.textView setLimitTextLengthBlock:^{
        
    }];
    
    [self.textContainer addSubview:self.textView];
    
    self.textView.layer.cornerRadius = 5.f;
    self.textView.layer.masksToBounds = YES;
    self.textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textView.layer.borderWidth = 0.5f;
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.textContainer);
    }];
    
}

- (IBAction)saveBtnClick:(id)sender {
    if (self.textView.text.length < 1 || self.recordId.length < 1) {
        Show_Msg_Error(@"请填写处理意见")
        return;
    }
    
    [self requestData];
}

- (void)requestData {
    BMWeakSelf
    Show_Msg(@"处理中")
    [HttpRequest handleWarnWithRecordId:self.recordId?:@"" text:self.textView.text success:^(id object) {
        Show_Msg_Success(@"处理成功")
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (weakSelf.BlockHandleSuccess) {
                weakSelf.BlockHandleSuccess(weakSelf.textView.text);
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
    }];
}

// 监听输入框内的文字变化
- (void)YCTextView:(YCTextView *)YCTextView textDidChanged:(NSString *)text {
    
}

@end
